package com.xyhy.core.base.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Persistable;

import com.xyhy.core.base.converter.DTOConverter;
import com.xyhy.core.base.model.dto.ObjectDTO;
import com.xyhy.core.base.service.ICrudService;

public abstract class DetailDrPoController<PO extends Persistable<ID>, ID, //
        SERVICE extends ICrudService<PO, ID>, DTO extends ObjectDTO<ID>, //
        CONVERTER extends DTOConverter<PO, DTO>, //
        DetailDTO extends ObjectDTO<ID>, DETAILCONVERTER extends DTOConverter<PO, DetailDTO>> //
        implements ICrudController<DTO, PO, DetailDTO, ID> {

  protected @Autowired SERVICE service;
  protected @Autowired CONVERTER converter;
  protected @Autowired DETAILCONVERTER detailConverter;

  @Override
  public DetailDTO findById(ID id) {
    return detailConverter.convert(service.findById(id));
  }

  @Override
  public PO add(DTO data) throws Exception {
    return this.save(data);
  }

  @Override
  public PO update(DTO data) throws Exception {
    return this.save(data);
  }

  @Override
  public ID delete(ID id) {
    service.delete(id);
    return id;
  }

  @Override
  public List<ID> batchDelete(List<ID> ids) {
    service.deleteInBatch(ids);
    return ids;
  }

  public PO save(DTO data) {
    return service.save(converter.revert(data));
  }

}
